<template>
  <div class="container">
    <div class="header">
      <el-input v-model="input" placeholder="请输入菜品名称" clearable>
        <i slot="prefix" class="el-input__icon el-icon-search"></i>
      </el-input>
      <div class="right">
        <el-link
          type="danger"
          :underline="false"
          @click="handleDelete(multipleSelection.map((item) => item.id))"
          >批量删除</el-link
        >
        <el-divider direction="vertical"></el-divider>
        <el-link type="primary" :underline="false" @click="handleChange(0)"
          >批量启售
        </el-link>
        <el-divider direction="vertical"></el-divider>
        <el-link type="danger" :underline="false" @click="handleChange(1)"
          >批量停售</el-link
        >
        <el-button
          icon="el-icon-plus"
          type="primary"
          @click="$router.push({ name: 'AddDish', query: { isEdit: false } })"
          >新建菜品</el-button
        >
      </div>
    </div>
    <el-table
      :data="tableData"
      style="width: 100%"
      ref="multipleTable"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="30"></el-table-column>
      <el-table-column prop="name" label="菜品名称"></el-table-column>
      <el-table-column label="图片">
        <template v-slot="{ row }">
          <img
            style="width: 50px; height: 50px"
            :src="`${env}/common/download?name=${row.image}`"
            alt=""
          />
        </template>
      </el-table-column>
      <el-table-column prop="categoryName" label="菜品分类"> </el-table-column>
      <el-table-column prop="price" label="售价">
        <template v-slot="{ row }">
          <span>￥{{ row.price / 100 }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="售卖状态">
        <template v-slot="{ row }">
          {{ row.status === 1 ? "启售" : "停售" }}
        </template>
      </el-table-column>
      <el-table-column prop="updateTime" label="最后操作时间">
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-link
            type="primary"
            @click="
              $router.push({
                name: 'AddDish',
                query: { isEdit: true, id: row.id },
              })
            "
            >修改
          </el-link>
          <el-divider direction="vertical"></el-divider>
          <el-link type="danger" slot="reference" @click="handleChange(row)">{{
            row.status ? "停售" : "启售"
          }}</el-link>
          <el-divider direction="vertical"></el-divider>
          <el-link
            type="danger"
            slot="reference"
            @click="handleDelete([row.id])"
            >删除</el-link
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageInfo.page"
      :page-sizes="[10, 20, 30, 50]"
      :page-size="pageInfo.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import { getDish, deleteDish, changeStatus } from "@/api/dish";
export default {
  data() {
    return {
      env: process.env.VUE_APP_BASE_API,
      input: "",
      tableData: [],
      pageInfo: {
        page: 1,
        pageSize: 10,
      },
      total: 0,
      checked: false,
      multipleSelection: [],
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      getDish({ ...this.pageInfo, name: this.input }).then((res) => {
        this.tableData = res.data.records;
        this.total = res.data.total;
      });
    },
    handleSizeChange(val) {
      this.pageInfo.pageSize = val;
      this.init();
    },
    handleCurrentChange(val) {
      this.pageInfo.page = val;
      this.init();
    },
    handleChange(row) {
      changeStatus({ id: row.id, status: +!row.status }).then((res) => {
        this.$message.success("账号状态改变成功");
        this.init();
      });
    },
    handleDelete(idsArr) {
      if (idsArr.length === 0) {
        this.$message.warning("请勾选");
        return;
      }
      this.$confirm("确认删除该菜品, 是否继续?", "确定删除", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      }).then(() => {
        deleteDish({ ids: idsArr.join() }).then((res) => {
          this.$message.success("删除成功");
        });
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      console.log(this.multipleSelection);
    },
  },
};
</script>

<style lang="scss" scoped>
.container {
  padding: 30px;

  .header {
    display: flex;
    justify-content: space-between;

    .el-input {
      width: 250px;
    }

    .el-link {
      padding-left: 15px;
      padding-right: 20px;
    }
  }
}
</style>
